package com.yhd.admin.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yhd.admin.bean.Role;
import com.yhd.admin.service.RoleService;
import com.yhd.admin.utils.ServiceMessage;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	@RequestMapping()
	public String listPage(){
		return "role";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Object list(Role role,int page,int rows){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", roleService.getCount(role));
		List<Role> roles = roleService.getRoles(role, page, rows);
		map.put("rows", roles);
		return map;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addPage(){
		return "role-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody ServiceMessage add(Role role){
		roleService.addRole(role);
		return ServiceMessage.ok();
	}
	
	@RequestMapping(value="/check/name",method=RequestMethod.GET)
	public @ResponseBody ServiceMessage add(String name){
		Role role = roleService.getRoleByName(name);
		return ServiceMessage.ok(role);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody ServiceMessage update(Role role){
		roleService.updateRole(role);
		return ServiceMessage.ok();
	}
}
